//|||||||||||||||||||||||||||||||||||||||||||||||

#include "MHXViewer/MHXViewer.h"

#include <OgreLight.h>
#include <OgreWindowEventUtilities.h>

//|||||||||||||||||||||||||||||||||||||||||||||||

MHXViewer::MHXViewer()
{
	m_pCubeNode			= 0;
	m_pCubeEntity		= 0;
	m_pMHX              = 0;
	m_pLight            = 0;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

MHXViewer::~MHXViewer()
{
	//delete OgreFramework::getSingletonPtr();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MHXViewer::start()
{
	new OgreFramework();
	OgreFramework::getSingletonPtr()->initOgre("MHXViewer v1.0", this, 0);
	
	m_bShutdown = false;

	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Viewer initialized!");

	setupScene();
	run();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MHXViewer::setupScene()
{
	//OgreFramework::getSingletonPtr()->m_pSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox");

	m_pLight = OgreFramework::getSingletonPtr()->m_pSceneMgr->createLight("Light");
	m_pLight->setType(Ogre::Light::LT_POINT);
	m_pLight->setPosition(1.0, 12.0, 5.0);
	//m_pLight->setDirection(0.5, -1, -0.5);
	m_pLight->setDiffuseColour(0.5, 0.5, 0.5);
	m_pLight->setSpecularColour(0.5, 0.5, 0.5);
	m_pMHX = new OgreMHX("../media/MHX/human.mhx");

	m_pCubeEntity = OgreFramework::getSingletonPtr()->m_pSceneMgr->createEntity("MHXModel", "Human");
	m_pCubeEntity->setCastShadows(true);
	//m_pCubeEntity->rec
	m_pCubeNode = OgreFramework::getSingletonPtr()->m_pSceneMgr->getRootSceneNode()->createChildSceneNode("CubeNode");
	m_pCubeNode->attachObject(m_pCubeEntity);

}

//|||||||||||||||||||||||||||||||||||||||||||||||

void MHXViewer::run()
{
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Start main loop...");
	
	double timeSinceLastFrame = 0;
	double startTime = 0;

	OgreFramework::getSingletonPtr()->m_pRenderWnd->resetStatistics();
	
	while(!m_bShutdown && !OgreFramework::getSingletonPtr()->isOgreToBeShutDown()) 
	{
		if(OgreFramework::getSingletonPtr()->m_pRenderWnd->isClosed())m_bShutdown = true;

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
		Ogre::WindowEventUtilities::messagePump();
#endif	
		if(OgreFramework::getSingletonPtr()->m_pRenderWnd->isActive())
		{
			startTime = OgreFramework::getSingletonPtr()->m_pTimer->getMillisecondsCPU();
					
			OgreFramework::getSingletonPtr()->m_pKeyboard->capture();
			OgreFramework::getSingletonPtr()->m_pMouse->capture();

			OgreFramework::getSingletonPtr()->updateOgre(timeSinceLastFrame);
			OgreFramework::getSingletonPtr()->m_pRoot->renderOneFrame();
		
			timeSinceLastFrame = OgreFramework::getSingletonPtr()->m_pTimer->getMillisecondsCPU() - startTime;
		}
		else
		{
			Sleep(1000);
		}
	}

	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Main loop quit");
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Shutdown OGRE...");
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool MHXViewer::keyPressed(const OIS::KeyEvent &keyEventRef)
{
	OgreFramework::getSingletonPtr()->keyPressed(keyEventRef);
	
	if(OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_F))
	{
		 //do something
	}

	return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool MHXViewer::keyReleased(const OIS::KeyEvent &keyEventRef)
{
	OgreFramework::getSingletonPtr()->keyReleased(keyEventRef);
	
	return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||
